﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Reflection;

namespace MvcShop.Html
{/*
    public static class jqGridExtensions
    {
        public static string jqGrid<T>(this HtmlHelper helper, string gridID, string caption, string url)
        {
            if (gridID.Substring(0, 1) != "#")
                gridID = "#" + gridID;
            string pagerID = string.Format("{0}_pager", gridID);
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(" <script type=\"text/javascript\">$(function(){");//jQuery(document).ready(function() {
            sb.AppendLine("$('%%GRIDID%%').jqGrid({".Replace("%%GRIDID%%", gridID));       //jQuery("#list").jqGrid({
            sb.AppendFormat("url:'{0}',", url);                            //        url: '/Home/GridData/',
            sb.Append("datatype: 'json',mtype: 'GET',");                 //        datatype: 'json',mtype: 'GET',
            sb.AppendFormat("colNames:[{0}],", GetColNames<T>());

            sb.AppendFormat("colModel:[{0}],", GetColModel<T>());
            sb.Append("pager: '%%GRIDPAGERID%%',rowNum: 20,rowList: [10, 20, 50,100],".Replace("%%GRIDPAGERID%%", pagerID));
            sb.AppendFormat("sortname:'{0}',sortorder: 'desc',", GetSortField<T>());
            sb.Append("viewrecords: true,imgpath: '/themes/redmond/images',");
            sb.AppendFormat("caption: '{0}'", caption);
            sb.Append("});\n$('%%GRIDID%%').jqGrid('navGrid','%%GRIDPAGERID%%',{ edit: false, add: false, del: false });".Replace("%%GRIDID%%", gridID).Replace("%%GRIDPAGERID%%", pagerID));
            sb.Append("});</script>\n");
            sb.AppendFormat("<table id=\"{0}\" class=\"scroll\" cellpadding=\"0\" cellspacing=\"0\"></table>", gridID.Substring(1));
            sb.AppendFormat("<div id=\"{0}\" class=\"scroll\" style=\"text-align:center;\"></div>", pagerID.Substring(1));
            sb.AppendLine();
            return sb.ToString();
        }
        
        private static string GetColModel<T>()
        {
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(T));

            StringBuilder sb = new StringBuilder();
            int width=100;
            foreach (ModelMetadata proMeta in metadata.Properties)
            {
                ColWidthAttribute colWidthAttr = GetCustomAttribute<ColWidthAttribute>(proMeta) as ColWidthAttribute;
                if (colWidthAttr != null)
                    width = colWidthAttr.Width;
                sb.Append("{");
                sb.AppendFormat("name:'{0}',index:'{0}',width:{1},align:'left'", proMeta.PropertyName, width);
                sb.Append("},");
            }
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
            //return "{ name: 'Id', index: 'Id', width: 140, align: 'left' }, { name: 'Votes', index: 'Votes', width: 180, align: 'left' },{ name: 'Title', index: 'Title', width: 400, align: 'left',editable:true}";
        }

        private static object GetCustomAttribute<T>(ModelMetadata proMeta) 
        {
            PropertyInfo property = proMeta.ContainerType.GetProperty(proMeta.PropertyName);
            object[] propertyAttributes = property.GetCustomAttributes(typeof(T), true);
            if (propertyAttributes.Length > 0)
            {
                return (propertyAttributes[0]);

            }
            
            return null;
        }
    }

    public class ColWidthAttribute : Attribute
    {
        public ColWidthAttribute()
        {

        }

        public int Width { get; set; }
    }
  * */
}
